#include <bits/stdc++.h>

using namespace std;

#define FR freopen("in.txt", "r", stdin)

typedef long long ll;

struct Edge
{
    int from;
    int to;
    int nxt;
};

struct Graph
{
    Edge edges[10005];
    int head[10005];
    int tot;

    void add(int u, int v)
    {
        tot++;
        edges[tot].from = u;
        edges[tot].to = v;
        edges[tot].nxt = head[u];
        head[u] = tot;
    }
} graph, scc;

int dfn[10005];
int low[10005];
int sccId[10005];
bool inSta[10005];
int sccTot;
int tmp;
stack<int> sta;

int n, m;

void tarjan(int idx)
{
    dfn[idx] = low[idx] = ++tmp;
    sta.push(idx);
    inSta[idx] = true;
    for (int ne = graph.head[idx]; ne != 0; ne = graph.edges[ne].nxt)
    {
        if (dfn[graph.edges[ne].to] == 0)
        {
            // forward edge
            tarjan(graph.edges[ne].to);
            low[idx] = min(low[idx], low[graph.edges[ne].to]);
        }
        else if (inSta[graph.edges[ne].to])
        {
            // backward edge
            low[idx] = min(low[idx], dfn[graph.edges[ne].to]);
        }
    }

    if (dfn[idx] == low[idx])
    {
        // scc
        sccTot++;
        int curr = 0;
        do
        {
            curr = sta.top();
            sta.pop();
            // update scc
            inSta[curr] = false;
            sccId[curr] = sccTot;
        } while (curr != idx);
    }
}

stack<int> topoSta;
bool vis[10005];
void topo(int u)
{
    vis[u] = true;
    for (int ne = scc.head[u]; ne != 0; ne = scc.edges[ne].nxt)
        if (!vis[scc.edges[ne].to])
            topo(scc.edges[ne].to);

    topoSta.push(u);
}

void scc_topo()
{
    for (int i = 1; i <= n; i++)
        if (dfn[i] == 0)
            tarjan(i);

    for (int i = 1; i <= graph.tot; i++)
        if (sccId[graph.edges[i].from] != sccId[graph.edges[i].to])
            scc.add(sccId[graph.edges[i].from], sccId[graph.edges[i].to]);

    for (int i = 1; i <= sccTot; i++)
        if (!vis[i])
            topo(i);
}

int main()
{
    while (!topoSta.empty())
    {
        int u = topoSta.top();
        topoSta.pop();
        // update ans
        for (int ne = scc.head[u]; ne != 0; ne = scc.edges[ne].nxt)
        {
            // update to
        }
    }
    return 0;
}